// pages/czqhomedetail/czqhomedetail.js
// pages/bureau/bureau.js
// const request = require('../../utils/request.js');
import { picUrl, formatTime, dataHandleNull } from '../../utils/util.js';
import { activityDetail } from '../../utils/getData.js'
const getopen = require('../../utils/maxutil.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    minModel:app.globalData.minModel, //小机型iphone8
    
    headerStatus:3, //1:箭头分离头像,2箭头头像,3头像关闭图标 头部状态
    avatarstatus:1,
    activityId:'',//局详情数据id
    conventiondata:'', //局详情数据
     isloding:false, //加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let conventionId = '';
    //扫码进来
    if(options.scene && options.scene){ 
      conventionId = options.scene
    }
    //页面内跳入
    if(options.activityId && options.activityId){
      conventionId = options.activityId;
    }
    //是否登录
    if(wx.getStorageSync('userId')){
      this.getdetail(conventionId);
    }else{
      this.login(conventionId);
    }
  },
  //登录
  login(conventionId){
    let than = this;
    getopen.loginOpenid().then(res=> {
      console.log('取到了openid')
      this.getdetail(conventionId);
    })
  },

  getdetail(conventionId){
    let than = this;
   let data = {
     userId:wx.getStorageSync('userId'),
     activityId:conventionId,
   }

   activityDetail(data).then( function (res){
     console.log(res)
     if(res.success){
       let item = dataHandleNull(res.result); 


       //数据处理

       //判断局是否结束
       let currentTime = formatTime(new Date()); //当前时间
       currentTime = currentTime.substring(0, currentTime.length - 9);
       let activityMediaArr = item.activityMedia.split(',');
       activityMediaArr = activityMediaArr.map((v)=>{
        return picUrl('activity/img/', v)
       })
       let  userTagName ;
       
       let activityEndTime = item.activityDate.slice(0,10).replace(/-/g, "/"); //局结束时间
          console.log(currentTime > activityEndTime)
          let payWay = "所有人AA";
          if(item.activityPayWay==2){
            payWay = "男生AA";
          }else if(item.activityPayWay==3){
            payWay = "女生AA";
          }else if(item.activityPayWay==4){
            payWay = "我请客";
          }
          let tt = "￥"+item.activityAmount+"/人"+"\xa0\xa0\xa0"+payWay;
          
          
          let newDatd = {
            ...item,
            activitytype:1,//0默认首页1局详情2活动详情
            activityMediaArr:activityMediaArr,//轮播图
            activityInvalidation: currentTime > activityEndTime, //局是否失效
            activityBlurCoverPage: picUrl('activity/img/', item.activityBlurCoverPage),
            activityCoverPage: picUrl('activity/img/', item.activityCoverPage),
            activityAddress: item.activityAddress.split(',')[0],
            activityBeginTime: item.activityBeginTime,
            activityDate: item.activityDate.slice(5).replace(/-/g, "/"),
            activityEndTime: item.activityEndTime,
            activityId: item.activityId,
            activityImageRGB: item.activityRGB, //
            activityJoinType: item.activityJoinType,
            activityMaxNums: item.activityMaxNums,
            activityName: item.activityName,
            activityRGB: item.activityRGB,
            activityType: item.activityType, //是否为大型活动
            islock: false, //是否为第一次跳转大型活动， false--未锁定  true--锁定
            activityMoney:tt, //
            guanzhu: item.guanzhu, //
            guanzhuTips: item.guanzhuTips, //
            userTips: item.userTips, //
            userTagNames:item.userTagNames,//标签
            userTagName:"蹦迪",
            join: item.activityType ==1 ? false :item.join, //
            shareImg: picUrl('activity/img/', item.shareImg), //
            activityUsedNums: item.activityUsedNums,
            activityUserMap:item.activityUserMap.participantlist,

            sponsorId: item.sponsorId,
            sponsorUser: {
              ...dataHandleNull(item.sponsorUser),
              avatarPath: picUrl('', item.sponsorUser.avatarPath),
              authImgPath: picUrl('activity/img/', item.sponsorUser.authImgPath),
              userPic: picUrl('activity/img/', item.sponsorUser.userPic)
            },

          }
 
        let newDatdtwo = {
          ...newDatd,
          activityUserMap: newDatd.activityUserMap && newDatd.activityUserMap.map((c) => {
            return {
              ...dataHandleNull(c),
              authImgPath: picUrl('', c.authImgPath),
              avatarPath: picUrl('', c.avatarPath),
              userPic: picUrl('', c.userPic),
              userTags:c.userTags?c.userTags.split(","):[]
            }
          })
        }

      
          //传给头部headers组件的数据
          let headerdata = {
            conventionId:newDatdtwo.activityId,
            
            islike:newDatdtwo.guanzhu,
            shareImg:newDatdtwo.shareImg
          }
          //局汇总需要的基础数据，避免重复打接口
          let conventiondata = {
            ...newDatdtwo,
            conventionId:newDatdtwo.activityId,
            avatarstatus:newDatdtwo.avatarstatus,
            conventionName:newDatdtwo.activityName,
            conventionDate:newDatdtwo.activityDate,
            conventionBeginTime:newDatdtwo.activityBeginTime,
            conventionEndTime:newDatdtwo.activityEndTime,
            conventionAddress:newDatdtwo.conventionAddress,
            conventionCoverPage:newDatdtwo.conventionCoverPage,
            conventionBlurCoverPage:newDatdtwo.conventionBlurCoverPage,
            conventionImageRGB:newDatdtwo.conventionImageRGB,
            conventionInvalidation:currentTime >= activityEndTime, //活动是否失效
            // conventionInvalidation:false, //未结束
            headerdata
          }
          app.globalData.activeJuInfo = conventiondata;

          than.setData({
            conventiondata:conventiondata,
            isloding:true,
            // headerdata,
          })
     }
   })
 },
 _maxCardClose(){
   console.log('页面回调了')
 },


  /**
   * 用户点击分享
   */
  onShareAppMessage: function () {
    return {
      path: 'pages/bureau/bureau?conventionId='+this.data.conventiondata.conventionId,
      imageUrl: this.data.conventiondata.shareImg,
    };
    // 用户点击右上角分享给好友,要先在分享好友这里设置menus的两个参数,才可以分享朋友圈
    wx.showShareMenu({
     withShareTicket: true,
     menus: ['shareAppMessage', 'shareTimeline']
   })
  },

  //用户点击右上角分享朋友圈
  onShareTimeline: function () { 
    return {
        path: 'pages/bureau/bureau?conventionId='+this.data.conventiondata.conventionId,
        imageUrl: this.data.list[this.data.current].shareImg,
      }
  },
  
})